﻿#include <iostream>
#include <string>
#include <sstream>
#include "NaiveMcC.h"
#include "CStopWatch.h"
#include <algorithm>

using namespace std;
void print(Node * n, string space, string* pattern);




int main(int argc, char *argv[])
{

	if (argc != 3){
		cout << "Wrong number of arguments, there must be exactly 2!" << endl;
		return 1;
	}

	string line;
	ostringstream sum;
	ifstream inputFile(argv[1]);

	//Check if the file can open
	if (inputFile.is_open()){

		//Read the file line by line
		while (getline(inputFile, line)){

			//save the lines to the string sum
			sum << line << "\n";
		}
		sum << "$";
		//close filestream
		inputFile.close();

		NaviveMcC McC;
		Node root;
		string pattern = sum.str();
		root.pattern = &pattern;
		CStopWatch watch;
		//watch.startTimer();
		McC.CreateTree(&pattern, &root);
		//watch.stopTimer();
		

		vector<int> result;
		watch.startTimer();
		McC.Search(argv[2], &result, &root);
		watch.stopTimer();
		sort(result.begin(), result.end());

		cout << "Time: " << watch.getElapsedTime() << " microsesconds" << endl << endl;
		for each (int value in result)
		{
			cout << value << " ";
		}
		cout << endl;
		
		//print(&root,"",&pattern);



	}
	else
		cout << "couldn't open file" << endl;

	


	//cout << hej << endl;
	return 0;

}

void print(Node * n, string space, string* pattern)
{
	string number = (n->suffix != -1) ? ": " + to_string(n->suffix + 1) : "";
	cout << space << n->getEdge(pattern)/* << "(" << n->l << "," << n->r << ")"*/<< number << endl;
	
	for each (Node* ni in n->Children)
	{
		string indent;
		for (int u = 0; u < space.length(); u++)
			indent += " ";
		print(ni,indent+"|--",pattern);
	}


}
